package com.blkj.iam.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blkj.iam.common.annotation.Log;
import com.blkj.iam.common.annotation.RepeatSubmit;
import com.blkj.iam.common.enums.LogModuleEnum;
import com.blkj.iam.common.model.Option;
import com.blkj.iam.common.result.PageResult;
import com.blkj.iam.common.result.Result;
import com.blkj.iam.system.model.form.TenantForm;
import com.blkj.iam.system.model.query.TenantPageQuery;
import com.blkj.iam.system.model.vo.TenantPageVO;
import com.blkj.iam.system.service.TenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 租户控制层
 * @Author: jeecg-boot
 * @Date: 2025-04-10
 * @Version: V1.0
 */
@Tag(name = "14.租户接口")
@RestController
@RequestMapping("/api/v1/tenant")
@Slf4j
public class TenantController {

    @Resource
    private TenantService tenantService;


    @Operation(summary = "租户分页列表")
    @GetMapping("/page")
    @Log(value = "租户分页列表", module = LogModuleEnum.TENANT)
    public PageResult<TenantPageVO> getTenantPage(@Valid TenantPageQuery queryParams) {
        IPage<TenantPageVO> result = tenantService.getTenantPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "应用下拉列表")
    @GetMapping("/options")
    public Result<List<Option<Long>>> getClientOptions() {
        List<Option<Long>> list = tenantService.listClientOptions();
        return Result.success(list);
    }

    @Operation(summary = "租户表单数据")
    @GetMapping("/{tenantId}/form")
    @Log(value = "租户表单数据", module = LogModuleEnum.TENANT)
    public Result<TenantForm> getTenantForm(@Parameter(description = "租户ID") @PathVariable Long tenantId) {
        TenantForm formData = tenantService.getTenantFormData(tenantId);
        return Result.success(formData);
    }

    @Operation(summary = "新增租户")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:tenant:add')")
    @RepeatSubmit
    @Log(value = "新增租户", module = LogModuleEnum.TENANT)
    public Result<?> saveTenant(@RequestBody @Valid TenantForm tenantForm) {
        boolean result = tenantService.saveTenant(tenantForm);
        return Result.judge(result);
    }

    @Operation(summary = "修改租户")
    @PutMapping(value = "/{tenantId}")
    @PreAuthorize("@ss.hasPerm('sys:tenant:edit')")
    @Log(value = "修改租户", module = LogModuleEnum.TENANT)
    public Result<Void> updateTenant(@Parameter(description = "租户ID") @PathVariable Long tenantId, @RequestBody @Valid TenantForm tenantForm) {
        boolean result = tenantService.updateTenant(tenantId, tenantForm);
        return Result.judge(result);
    }

    @Operation(summary = "删除租户")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:tenant:delete')")
    @Log(value = "删除租户", module = LogModuleEnum.TENANT)
    public Result<Void> deleteTenants(@Parameter(description = "租户ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        boolean result = tenantService.deleteTenants(ids);
        return Result.judge(result);
    }
}
